package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.EnrollStudentSpeech;
import com.xmy.cultivate.service.IEnrollStudentSpeechService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.ExcelUtil;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/enrollStudentSpeech")
@Scope("prototype")
public class EnrollStudentSpeechController extends BaseController {
    @Autowired
    IEnrollStudentSpeechService iEnrollStudentSpeechService;
    @GetMapping()
    public Result getList(@RequestParam Map<String, String> reMap){
        IPage<EnrollStudentSpeech> iPage = getListData(reMap);
        return new Result(Code.OK,new PageResult<>(iPage.getRecords(),iPage.getTotal(),iPage.getPages()));
    }
    @PostMapping()
    public Result goToSave(@RequestBody EnrollStudentSpeech enrollStudentSpeech){
        enrollStudentSpeech.setDeleted(0);
        iEnrollStudentSpeechService.save(enrollStudentSpeech);
        return new Result(Code.OK,"","");
    }
    @PutMapping()
    public Result goToEdit(@RequestBody EnrollStudentSpeech enrollStudentSpeech){
        iEnrollStudentSpeechService.updateById(enrollStudentSpeech);
        return new Result(Code.OK,"","");
    }
    @DeleteMapping()
    public Result goToDel(@RequestParam Map<String, String> reMap){
        String id = reMap.get("id");
        if (id == null || id.isEmpty()) {
            return new Result(Code.ERROR,"","id不能为空！");
        }
        iEnrollStudentSpeechService.remove(new QueryWrapper<EnrollStudentSpeech>().inSql("id", id));
        return new Result(Code.OK,"","");
    }
    @GetMapping("excel")
    public Result getListExcel(@RequestParam Map<String, String> reMap){
        IPage<EnrollStudentSpeech> iPage = getListData(reMap);

        String fileName = "话术设置";

        ExcelUtil.export(fileName, iPage.getRecords(), EnrollStudentSpeech.class, response);
        return new Result(Code.OK,null,"导出成功");
    }

    private IPage<EnrollStudentSpeech> getListData(Map<String, String> reMap) {
        int pageNum = StringUtils.isEmpty(reMap.get("pageNum")) ? 1 : Integer.parseInt(reMap.get("pageNum"));
        int pageSize = StringUtils.isEmpty(reMap.get("pageSize")) ? 10: Integer.parseInt(reMap.get("pageSize"));
        IPage<EnrollStudentSpeech> iPage = new Page<>(pageNum,pageSize);
        String studentOptionId = reMap.get("studentOptionId");

        QueryWrapper<EnrollStudentSpeech> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*,created_at AS addTime,(SELECT name FROM enroll_student_option WHERE id = enroll_student_speech.student_option_id LIMIT 1) AS studentOptionName");
        queryWrapper.eq(StringUtils.isNotBlank(studentOptionId),"student_option_id",studentOptionId);
        queryWrapper.orderByAsc("sort,id");
        iEnrollStudentSpeechService.page(iPage, queryWrapper);
        return iPage;
    }
}
